package com.shell.governor.modules.service.impl;

import com.shell.bee.entity.auth.AuthUser;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.bee.persistence.model.PageModel;
import com.shell.governor.modules.po.PubMessage;
import com.shell.governor.modules.po.PubNotice;
import com.shell.governor.modules.service.MsgTipService;
import com.shell.governor.modules.service.PubMessageService;
import com.shell.governor.modules.service.PubNoticeService;
import com.shell.governor.user.dao.PubUserDao;
import com.shell.governor.user.vo.PubUserVo;
import com.shell.governor.user.vo.UserQueryVo;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MsgTipServiceImpl implements MsgTipService {
	@Autowired
	private PubMessageService pubMessageService;
	@Autowired
	private PubNoticeService pubNoticeService;
	@Autowired
	private PubUserDao pubUserDao;

	public int getNotReadedMsgCount(String userId) throws Exception {
		PageLimit limit = new PageLimit();
		limit.setPageNo(1);
		limit.setPageSize(1);
		PubMessage msg = new PubMessage();
		msg.setReceiverId(userId);
		msg.setMessageTypeCode("notReaded");
		PageModel<PubMessage> pageModel = (PageModel) this.pubMessageService.getMyMessagesByPage(limit, msg);
		return (int) pageModel.getTotal();
	}

	public int getNoticeCount() {
		PubNotice notice = new PubNotice();
		return this.pubNoticeService.getAllNotices(notice).size();
	}

	public int getUserAprCount(String type, AuthUser authUser) {
		UserQueryVo user = new UserQueryVo();

		Map<String, Object> group = (Map) authUser.getExtension().get("group");
		if ("0".equals(type)) {
			String[] States = { "0", "3" };

			user.setStates(States);

			List<Map<String, String>> m = (List) group.get("firstTrialGroups");
			if ((m != null) && (m.size() > 0)) {
				String[] grpIds = new String[m.size()];
				int i = 0;
				for (Map<String, String> grp : m) {
					grpIds[i] = ((String) grp.get("grpId"));
				}
				user.setGrpIds(grpIds);
			}
		} else {
			String[] States = { "1" };

			user.setStates(States);

			List<Map<String, String>> m = (List) group.get("lastTrialGroups");
			if ((m != null) && (m.size() > 0)) {
				String[] grpIds = new String[m.size()];
				int i = 0;
				for (Map<String, String> grp : m) {
					grpIds[i] = ((String) grp.get("grpId"));
				}
				user.setGrpIds(grpIds);
			}
		}
		List<PubUserVo> users = this.pubUserDao.selectUserForApr(user);
		return users.size();
	}
}
